//
//  WACommentListController.m
//  WalkiOS
//
//  Created by luqiuan on 2019/11/10.
//  Copyright © 2019 Chenmuzhou. All rights reserved.
//

#import "WACommentListController.h"
#import "WAVCommentCell.h"
#import "WACommentModel.h"
#import "WACommentDetailController.h"

@interface WACommentListController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (assign, nonatomic) NSInteger pageNum;
@property (strong, nonatomic) NSMutableArray *dataSouces;
@end

@implementation WACommentListController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"WAVCommentCell" bundle:nil] forCellReuseIdentifier:@"WAVCommentCell"];
    
    @weakify(self)
    self.tableView.mj_header = [MJRefreshLoadHeader headerWithRefreshingBlock:^{
        @strongify(self)
        self.pageNum = 1;
        [self requestData];
    }];
    self.tableView.mj_footer = [MJRefreshMoreFooter footerWithRefreshingBlock:^{
        @strongify(self)
        self.pageNum ++;
        [self requestData];
    }];
    self.tableView.delegate = self;
//    self.tableView.tableFooterView = [UIView new];
    [self.tableView.mj_header beginRefreshing];
}

- (void)requestData{
    [WNBusinessRequest getCommentInfoWithPageNum:self.pageNum Succ:^(id  _Nonnull responseObject) {
        NSDictionary *dict = (NSDictionary *)responseObject;
        NSInteger total = [dict[@"total"] integerValue];
        NSArray *memberList = [WACommentModel mj_objectArrayWithKeyValuesArray:dict[@"data"]];
        if (self.pageNum == 1) {
            self.dataSouces = [NSMutableArray new];
        }
        [self.dataSouces addObjectsFromArray:memberList];
        if(total <= self.dataSouces.count){
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
        [self.tableView.mj_header endRefreshing];
        if (self.dataSouces.count == 0) {
            self.noDataStr = @"无数据";
        }else{
            [self.tableView reloadData];
        }
        [self.tableView reloadData];
        [self.tableView reloadEmptyDataSet];
        
    } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
        NSLog(@"");
        [self.tableView reloadEmptyDataSet];
        [WNTool show:mes];
    }];
}

#pragma mark - UITableView Delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WAVCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WAVCommentCell"];
    WACommentModel *model = self.dataSouces[indexPath.row];
    cell.contentLB.text = [model.content removeSpaceAndNewline];;
    [cell setModel:model];
    @weakify(self)
    [cell setClickABlock:^(WACommentModel * _Nonnull model) {
        @strongify(self)
        WACommentDetailController *vc = [[WACommentDetailController alloc] initWithModel:model];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSouces.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;//高度最佳为180
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    WACommentModel *model = self.dataSouces[indexPath.row];
    
}


#pragma mark - DZNEmptyDataSetSource
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view{
    [self.tableView.mj_header beginRefreshing];
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView{
    return [WNTool requestErrorWithSting:self.noDataStr];
}

- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
    
}


@end
